package org.rui;

import lombok.extern.slf4j.Slf4j;
import org.rui.example.common.service.UserService;
import org.rui.provider.service.impl.UserServiceImpl;
import org.rui.rpc.config.RegistryConfig;
import org.rui.rpc.config.RpcConfig;
import org.rui.rpc.model.ServiceMetaInfo;
import org.rui.rpc.registry.LocalRegistry;
import org.rui.rpc.registry.Registry;
import org.rui.rpc.registry.RegistryFactory;
import org.rui.rpc.server.tcp.VertxTcpServer;
import org.rui.rpc.service.HttpServer;
import org.rui.rpc.service.VertxHttpServer;

/**
 * @author: linweirui
 * @date: 2024/12/02 10:53
 */

/**
 * 简易服务提供者示例
 */
@Slf4j
public class EasyProviderApplication {
    public static void main(String[] args) {
        //根据resource目录下的配置文件加载配置 如果没有则加载默认配置
        RpcConfig rpcConfig = RpcApplication.getRpcConfig();
        log.info("provider rpcConfig is {} ",rpcConfig.toString());
        //注册服务
        LocalRegistry.registry(UserService.class.getName(), UserServiceImpl.class);
        //注册服务到注册中心
        RegistryConfig registryConfig = rpcConfig.getRegistryConfig();
        //通过spi机制获取注册中心实例
        Registry registry = RegistryFactory.getInstance(registryConfig.getRegistry());
        //封装服务信息
        ServiceMetaInfo serviceMetaInfo = new ServiceMetaInfo();
        serviceMetaInfo.setServiceName(UserService.class.getName());
        serviceMetaInfo.setServiceHost(rpcConfig.getServerHost());
        serviceMetaInfo.setServicePort(rpcConfig.getServerPort());
        try {
            //注册服务到第三方中间件 如etcd、zookeeper等
            registry.register(serviceMetaInfo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        // 启动 web 服务
        /*HttpServer httpServer = new VertxHttpServer();
        httpServer.doStart(rpcConfig.getServerPort());*/
        VertxTcpServer vertxTcpServer = new VertxTcpServer();
        vertxTcpServer.doStart(rpcConfig.getServerPort());
    }
}